package com.farsoon.childcommon.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

// 重写MetaObjectHandler接口实现 @TableField(value = "create_time", fill = FieldFill.INSERT, insertStrategy = FieldStrategy.NOT_NULL) 自动插入时间及更新时间
@Component
@Slf4j
public class MyBatisPlusObjectHandle implements MetaObjectHandler {

    public void insertFill(MetaObject metaObject) {
//        LoginUser user = LoginUser.getCurrentInstance();
//        if (Objects.nonNull(user)) {
//            setFieldValByName("createBy", user.getId(), metaObject);
//            setFieldValByName("createName", user.getUsername(), metaObject);
//        }
        setFieldValByName("createTime", new Date(), metaObject);
    }

    public void updateFill(MetaObject metaObject)
    {
//        LoginUser user = LoginUser.getCurrentInstance();
//        if (Objects.nonNull(user)) {
//            setFieldValByName("updateBy", user.getId(), metaObject);
//            setFieldValByName("updateName", user.getUsername(), metaObject);
//        }
        setFieldValByName("updateTime", new Date(), metaObject);
    }
}
